<template>
  <div class="login" style="height: 100%;" v-loading="pageLoading" :element-loading-text="loadingText">
    <!-- <div class="login-panel" id="loginPanel">
      <el-form
        hide-required-asterisk
        ref="loginForm"
        id="loginForm"
        :model="loginForm"
        :rules="loginRules"
        label-position="top"
      >
        <div class="logo">
          <h1>{{titlename}}</h1>
        </div>
        <el-form-item class="login-item" label="Account" prop="username">
          <el-input
            class="login-input"
            v-model="loginForm.username"
            placeholder="ACCOUNT"
            autocomplete="off"
          >
            <i slot="prefix" class="input-icon username"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="login-item" label="Password" prop="password">
          <el-input
            class="login-input"
            type="password"
            v-model="loginForm.password"
            placeholder="PASSWORD"
            autocomplete="off"
          >
            <i slot="prefix" class="input-icon password"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="login-item">
          <div class="login-feeling">
            <h2>How's your feeling today?</h2>
            <div class="login-emoji">
              <div class="icon-box"
                v-for="(item, index) in feelingClassName"
                :key="index"
                :class="[item, { 'active': index === isActive }]"
                @click="changeFeeling(item, index)"
              >
                <i class="icon"></i>
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item class="submit-item" style="margin-bottom: 1.5rem;">
          <el-button
           class="submit-btn"
           :loading="submitting"
           type="primary"
           @click="submitForm('loginForm')">Sign in</el-button>
        </el-form-item>
      </el-form>
      <div class="login-feelings" :class="[feeling]"></div>
    </div> -->
  </div>
</template>
<script>
// import md5 from 'md5'
// import { getLogin } from '@/api/user'
export default {
  name: 'Login',
  data () {
    return {
      token: '',
      userId: '',
      pageLoading: false,
      loadingText: '加载中...'
    }
  },
  mounted () {
    // this.userId = this.getQueryVariable('userId')
    this.token = this.getQueryVariable('token')
    if (window.location.href.indexOf(this.token) > 0) {
      this.getLoginTokenHandle()
    } else {
      const newUrl = process.env.VUE_APP_LOGIN_URL
      location.replace(newUrl)
    }
  },
  methods: {
    getQueryVariable (variable) {
      var query = window.location.search.substring(1)
      var vars = query.split('&')
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split('=')
        if (pair[0] === variable) { return pair[1] }
      }
      return (false)
    },
    getLoginTokenHandle () {
      this.pageLoading = true
      const params = {
        bocToken: this.token
      }
      this.$store.dispatch('LoginByLoginCode', params).then(res => {
        if (res) {
          if (Number(res.code) === 1) {
            let newUrl = location.href
            if (newUrl.indexOf('?')) {
              newUrl = newUrl.split('?')[0]
            }
            // const newUrl = location.href.replace(new RegExp('[?|&]userId=([^&;#]+)', 'i'), '')
            location.replace(newUrl)
          } else if (Number(res.code) === 104) {
            this.$confirm('boc服务出错', {
              confirmButtonText: '确定',
              showCancelButton: false
            })
              .then(() => {
                const newUrl = process.env.VUE_APP_LOGIN_URL
                location.replace(newUrl)
              })
          } else if (Number(res.code) === 105) {
            this.$confirm('服务器出错', {
              confirmButtonText: '确定',
              showCancelButton: false
            })
              .then(() => {
                const newUrl = process.env.VUE_APP_LOGIN_URL
                location.replace(newUrl)
              })
          } else {
            this.$confirm('请重新登录', {
              confirmButtonText: '确定',
              showCancelButton: false
            })
              .then(() => {
                const newUrl = process.env.VUE_APP_LOGIN_URL
                location.replace(newUrl)
              })
          }
        } else {
          this.$notify.error({
            title: '错误',
            message: res.msg
          })
        }
        this.pageLoading = false
      })
    }
  }
}
</script>
